
var Sched	= {
	XD:['日','一','二','三','四','五','六'],
	XM:['一','二','三','四','五','六','七','八','九','十','十一','十二'],
	Dates:false,
	isLoaded: false,
	delta: (3600*1000*12) ,
	htmlBody : null,

	Init: function(){
		if( this.isLoaded ){
			return ;
		}
		this.isLoaded	= true;

		var str	= '<table cellspacing="1" cellpadding="0">';

		str	+=	'<tbody class="Sched_Tools">';
			str	+= '<tr><td>?</td><th colspan="5" class="Sched_Title">2009</th><td>×</td></tr>';
			str	+= '<tr><td>&lt;&lt;</td><td>&lt;</td><td colspan="3" class="Sched_Tody">今天</th><td>&gt;</td><td>&gt;&gt;</td></tr>';
		str	+= '</tbody>';
	
		str	+= '<tbody><tr>';
			for(var i=0; i<7 ; i++ ){
				str += '<td>' + Sched.XD[i] + '</td>';
			}
		str	+= '</tr></tbody>';

		str	+= '<tbody class="Sched_Grid"><tr>';	
			for(var i=0; i<42 ; i++ ){
				if( (i)%7 == 0 && i)  str += "</tr><tr>";
				str += "<td>" + i + "</td>";
			}
		str	+= '</tr></tbody></table>';

		var _div	= new Element('div').set('id', 'scheduler').set('html', str);
		
		if( !!window.ActiveXObject ){
			new Element('iframe').setProperty('frameborder', 0).injectTop(_div);
		}

		_div.setStyle('display', 'none');

		if( !!$('html') ){
			_div.inject('html');
			this.htmlBody	= $('html');
		}else{
			_div.inject(document.body);
			this.htmlBody	= document.documentElement ;
		}

		this.addEvents();

		
		$$("input.scheduler").each(function(el){

			el.title		= "点击选择日期";
			el.addEvent('click',	Sched.Open);
			el.addEvent('keypress',	Sched.keyDown);

			el.onpaste		= function(){ return false};
			el.setAttribute('maxlength', 10);
		});

	},

	addEvents: function(){
		if( !!window.ActiveXObject ){
			$$('#scheduler td').addEvents({
				mouseenter: function(){
					this.addClass('hover');
				},
				mouseleave: function(){
					this.removeClass('hover');
				}
			});
		}

		$$('#scheduler .Sched_Grid td').addEvent('click', Sched.getVal);

		var o = $$("#scheduler .Sched_Tools td");
	
		o[0].addEvent('click', function(){
			alert('点击选择格式化日期!');
		});
		o[1].addEvent('click',  this.hide);
		o[2].addEvent('click',  function(){
			Sched.Dates	= new Date(Sched.Dates.getFullYear()-1 , Sched.Dates.getMonth(), Sched.Dates.getDate());
			Sched.setDate();
		});
		o[3].addEvent('click',  function(){
			Sched.Dates	= new Date(Sched.Dates.getFullYear() , Sched.Dates.getMonth()-1,Sched.Dates.getDate());
			Sched.setDate();
		});
		o[4].addEvent('click',  function(){
			Sched.Dates	=  new Date();
			Sched.PutVal( Sched.Dates.getDate() );
		});
		o[5].addEvent('click',  function(){
			Sched.Dates	= new Date(Sched.Dates.getFullYear() , Sched.Dates.getMonth()+1,Sched.Dates.getDate());
			Sched.setDate();
		});
		o[6].addEvent('click',  function(){
			Sched.Dates	= new Date(Sched.Dates.getFullYear()+1 , Sched.Dates.getMonth(),Sched.Dates.getDate());
			Sched.setDate();
		});
	},

	keyDown : function( evt ){
		if( evt.code < 48 || evt.code > 57 ){
			if( evt.code > 16 && evt.code!=39 && evt.code!=37 &&  evt.code!= 45 &&  evt.code != 32 ){
				evt.stop();
			}
		}
	},

	Open:function( evt ){

		Sched.oInpue	= this;
		var t	= this.value.split(/\D0*/);
		if(  t.length >= 3 ){
			var D	= new Date( parseInt(t[0]) , parseInt(t[1]) -1, parseInt(t[2]) ) ;
		}else{	
			var D	=  new Date();	
		}
		Sched.Dates	= D;

		var t2	= this.value.split(/\D0*/);

		var x	= parseInt(Sched.htmlBody.scrollLeft) +  evt.client.x + 4 ;	
		var y	= parseInt(Sched.htmlBody.scrollTop) + evt.client.y +  4 ;

		Sched.setDate();
		$('scheduler').setStyles({
			left: x ,
			top: y ,
			display : 'block'
		});
	},
	
	getVal:function(){
		var val	= parseInt(this.innerHTML);
		if( val ){
			Sched.PutVal( val );
		}
	},

	PutVal:function( d ){
		d		= String( parseInt(d) || 0 );
		d	= d.length > 1 ? '-' + d : '-0' + d ;

		var m	= String( Sched.Dates.getUTCMonth() +1 );
		m	= m.length > 1 ? '-' + m :'-0' + m ;

		Sched.oInpue.value	= Sched.Dates.getFullYear() + m +  d;
		Sched.hide();
	},

	hide: function(){
		$('scheduler').setStyles({
			display: 'none'
		});
	},

	setDate : function(){
		var t	= Sched.oInpue.defaultValue.split(/\D0*/).map(function(e){
			return parseInt(e);
		});
		if( t.length >= 3 ){
			t[1]--;
		}else{
			t	= [-1, -1, -1];
		}

		var	D	= this.Dates;
		var tody		= D.getDate();

		var D1			= new Date( D.getFullYear(), D.getMonth(), 1);
		var FirstDay	= D1.getDay();

		var D2			= new Date( D.getFullYear(), D.getMonth() + 1, 1);
		D2.setTime( D2.getTime() - Sched.delta );
		var lastDay		= D2.getDate() + FirstDay - 1;

		var isDefault	= false;

		if( D.getFullYear() == t[0] && D.getMonth() == t[1] ){
			isDefault	= true;
		}
		var grids	= $$('#scheduler .Sched_Grid td');
		grids.removeClass('now').removeClass('def');

		grids.each( function(el, i){
			if( i < FirstDay || i > lastDay ){
				el.set('text', ' ');
				el.className	= '';
			}else{
				var val	= i - FirstDay + 1;
				el.set('text', val );
				if( val == tody ){
					el.addClass('now');
				}
				if( isDefault && val == t[2] ){
					el.addClass('def');
				}
			}
		});
		delete grids;
		$$('#scheduler .Sched_Title').set('text', D.getFullYear() + '年 ' + this.XM[D.getUTCMonth()]+'月' );
	}
};